package cn.momoshenchi.medicine.handler;


import cn.momoshenchi.medicine.util.ParamsException;
import cn.momoshenchi.medicine.enumerate.ResultCode;
import com.fasterxml.jackson.core.JsonParseException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;

/**
 * @version : 1.0
 * @author: momoshenchi
 * @date: 2021/2/12 - 18:38
 */
@ControllerAdvice
public class GlobalExceptionHandler
{
    /**
     * 难点在于是否json
     *
     * @param e
     * @param request
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public ModelAndView handle(Exception e, HttpServletRequest request) throws Exception
    {
        e.printStackTrace();
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
        modelAndView.addObject("code", ResultCode.SYSTEM_ERROR.code());
        modelAndView.addObject("msg", ResultCode.SYSTEM_ERROR.message());

        if (e instanceof ParamsException)
        {
            ParamsException paramsException = (ParamsException) e;
            modelAndView.addObject("code", paramsException.getCode());
            modelAndView.addObject("msg", paramsException.getMsg());
        }
        else if (e instanceof TypeMismatchException)
        {
            modelAndView.addObject("code", ResultCode.PARAM_TYPE_BIND_ERROR.code());
            modelAndView.addObject("msg", ResultCode.PARAM_TYPE_BIND_ERROR.message());
        }
        else if (e instanceof BindException)
        {
            BindException bindException = (BindException) e;
            modelAndView.addObject("code", ResultCode.PARAM_IS_INVALID.code());
            modelAndView.addObject("msg", bindException.getBindingResult().getFieldError().getDefaultMessage());
        }
        else if (e instanceof HttpRequestMethodNotSupportedException)
        {
            modelAndView.addObject("code", ResultCode.REQUEST_TYPE_ERROR.code());
            modelAndView.addObject("msg", ResultCode.REQUEST_TYPE_ERROR.message());
        }
        else if (e instanceof JsonParseException)
        {
            modelAndView.addObject("code", ResultCode.PARAM_TYPE_BIND_ERROR.code());
            modelAndView.addObject("msg", ResultCode.PARAM_TYPE_BIND_ERROR.message());
        }
        else if (e instanceof HttpMessageConversionException)
        {
            modelAndView.addObject("code", ResultCode.PARAM_IS_BLANK.code());
            modelAndView.addObject("msg", ResultCode.PARAM_IS_BLANK.message());
        }
        return modelAndView;

    }
}
